package function

import (
	"fmt"
	"global/constants"
	"encoding/json"
)

// 获取队列的key
func GetQueueKey(coinPair string) string {
	return fmt.Sprintf(constants.TrustQueue, coinPair)
}

// 获取详情的 Key
func GetBuyTrustDetailKey(coinPair string) string {
	return fmt.Sprintf(constants.TrustBuyDetail, coinPair)
}

func GetSaleTrustDetailKey(coinPair string) string {
	return fmt.Sprintf(constants.TrustSaleDetail, coinPair)
}

// 等待交易的买家
func GetWaitBuyKey(coinPair string) string {
	return fmt.Sprintf(constants.TrustWaitBuy, coinPair)
}

// 等待交易的卖家
func GetWaitSaleKey(coinPair string) string {
	return fmt.Sprintf(constants.TrustWaitSale, coinPair)
}

// 订单的 key
func GetOrderKey(coinPair string) string {
	return fmt.Sprintf(constants.TrustOrderList, coinPair)
}

func GetDepthKey(coinPair, flag string, depth int) string {
	if flag == "buy" {
		return fmt.Sprintf(constants.TrustDepthBuy, coinPair, depth)
	}
	return fmt.Sprintf(constants.TrustDepthSale, coinPair, depth)
}

// 交易失败的数据
func GetTrustFailedListKey(coinPair string) string {
	return fmt.Sprintf(constants.TrustFailedList, coinPair)
}

// 获取用户买卖单地址的KEY
func GetUserTrustKey(flag string, coinPair string, uid int) string {
	if flag == "buy" {
		return fmt.Sprintf(constants.TrustUserBuyKey, coinPair, uid)
	}
	return fmt.Sprintf(constants.TrustUserSaleKey, coinPair, uid)
}

// 000000000000000000000000000000000000000000000000000000000
func GetKlineCacheMapKey(coinPair string) string {
	return fmt.Sprintf(constants.KlineCacheMapKey, coinPair)
}

func GetKlineCacheDataKey(coinPair string) string {
	return fmt.Sprintf(constants.KlineCacheDataKey, coinPair)
}

func GetKlineCacheLastKey(coinPair string) string {
	return fmt.Sprintf(constants.KlineCacheLastKey, coinPair)
}

// 000000000000000000000000000000000000000000000000000000000

// 获取缓存分析最后的 order id
func GetOrderLastIdKey() string {
	return constants.OrderLastOrderId
}

func GetOrderLatestAnalysisKey(coinPair string) string {
	return fmt.Sprintf(constants.OrderLatestAnalysis, coinPair)
}

// 用户成交单的KEY
func GetUserOrdersKey(coinPair string, uid int) string {
	return fmt.Sprintf(constants.UserHistoryOrderKey, coinPair, uid)
}

// 用户历史成交
func GetUserHistoryTrustKey(coinPair string, uid int) string {
	return fmt.Sprintf(constants.UserHistoryTrustKey, coinPair, uid)
}

func GetUserHistoryTrustWithoutCoinPairKey(uid int) string {
	return fmt.Sprintf(constants.UserHistoryTrustWithoutCoinPairKey, uid)
}

// 000000000000000000000000000000000000000000000000000000000

// 交易对
func CoinPairName(coinFrom, coinTo string) string {
	return fmt.Sprintf("%s_%s", coinFrom, coinTo)
}

func JsonEncode(data interface{}) (string, error) {
	result, err :=  json.Marshal(data)
	return string(result), err
}

// 000000000000000000000000000000000000000000000000000000000

func GetOteOrderList(coinPair string) string {
	return fmt.Sprintf(constants.OteOrderList, coinPair)
}
